package me.zhengjie.modules.security.service;


import lombok.extern.log4j.Log4j2;
import me.zhengjie.Consts;
import me.zhengjie.modules.security.service.dto.OnlineUserDto;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author Eric
 * @version 1.0.0
 * @create 2021-05-20 15:47
 * @since 1.0.0
 **/
@Log4j2
@Component
public class OnlineUserSubscribe {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OnlineUserService onlineUserService;

    @PostConstruct
    public void subscribe() {
        RTopic rTopic = redissonClient.getTopic(Consts.TOPIC_NAME);
        // 接受订阅的消息
        rTopic.addListener(OnlineUserDto.class, (charSequence, onlineUserDto) -> {
            log.info("接受到消息主题={}，内容={}", charSequence, onlineUserDto);
            onlineUserService.save(onlineUserDto);
        });
    }
}
